<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px;width: 100%"
  >
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operateDepts">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="add"
                :disabled="!checkPremission('add-dept')"
                >添加子部门</el-dropdown-item
              >
              <el-dropdown-item
                command="edit"
                v-if="!isRoot"
                :disabled="!checkPremission('set-dept')"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item
                command="del"
                v-if="!isRoot"
                :disabled="!checkPremission('del-dept')"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  props: {
    // 定义传入的属性
    treeNode: {
      require: true,
      type: Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    operateDepts (type) {
      if (type === 'add') {
        // 添加部门
        this.$emit('addDept', this.treeNode)
      } else if (type === 'edit') {
        // 修改部门
        this.$emit('editDept', this.treeNode)
      } else {
        this.$confirm('确定要删除该部门组织', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
          .then(() => {
            // console.log(this.treeNode.id)
            return delDepartment(this.treeNode.id)
          })
          .then(() => {
            // 告诉父组件我删除了
            this.$emit('delpots')
            this.$message.success('删除成功')
          })
      }
    }
  }
}
</script>

<style></style>
